<template>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="用户名" prop="name">
      <el-input v-model="ruleForm.name" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="pass">
      <el-input v-model="ruleForm.pass" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="权限" prop="limmits">
      <el-input v-model="ruleForm.limmits"></el-input>
    </el-form-item>
    <el-form-item label="权限列表" prop="l_mlist">
      <el-tree :data="asynrot" show-checkbox node-key="name"
      :props="defaultProps" ref="tree" @check-change="CheckChanges">
    </el-tree>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="add('ruleForm')">添加人员</el-button>
      <el-button @click="$emit('modify',ruleForm)">修改人员</el-button>
    </el-form-item>
    <!-- checkedKeys -->
  </el-form>
</template>
<script>
import {asyncRoutes} from '@/router'
export default {
  computed:{
    asynrot(){
      return asyncRoutes
    }
  },
  props:{
    ruleForm:{
      type:Object,
      default:()=>({})
    },
    
  },
  data() {
    var checkAge = (rule, value, callback) => {
      if (value === '') {
        return callback(new Error('权限不能为空'));
      } else if(typeof (value-1) === 'number' && value<=3){
        callback();
      }else {
        return callback(new Error('请输入1-3之间的数字'));
      }

    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('账户不能为空'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('密码不能为空'));
      } else {
        callback();
      }
    };
    return {
        defaultProps: {
          children: 'children',
          label: 'name'
        },
  
      rules: {
        name: [
          { validator: validatePass, trigger: 'blur' }
        ],
        pass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        limmits: [
          { validator: checkAge, trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    add(formName) {
      this.$emit('cli', this.ruleForm)
      this.$refs[formName].resetFields();
    },
    handleNodeClick(a,b,c){
      console.log(a,b,c);
    },
    getCheckedNodes(q) {
        console.log(q);
      },
      CheckChanges(){
      this.$emit('changes')
      // console.log(this.$refs.tree.getCheckedKeys(),this.ruleForm);
    }
  }
}
</script>
<style lang="scss" scoped>
  .demo-ruleForm{
    margin-top: 20px;
    margin-right: 20px;
  }
</style>